﻿<?php

/**
 *
 *
 *
 */
class TechnoDK_Autoloader extends TechnoDK_Object
{
	/**
	 *
	 *
	 *
	 */
	private $_classPaths;

	/**
	 *
	 *
	 *
	 */
	private static $_loader;
	
	/**
	 *
	 *
	 *
	 */
	private function __construct()
	{
		$this->addClassPath(__TECHNODKNAMESPACE__, dirname(__TECHNODKPATH__));
		spl_autoload_register(array($this,'loadClass'));
	}
	
	/**
	 *
	 *
	 *
	 */
	public static function initAutoloader()
	{
		if(isset(self::$_loader)){
			return self::$_loader;
		} else {
			self::$_loader = new self();
			return self::$_loader;
		}
	}
	
	/**
	 *
	 *
	 *
	 */
	public function addClassPath($namespace, $path)
	{
		if(!isset($classPaths)){
			$this->_classPaths = array();
		}
		$path = realpath($path);
		if(has_value($path)){
			$this->_classPaths[$namespace] = $path;
		} else {
			throw new Exception("Path doesn't exists");
		}
	}
	
	/**
	 *
	 *
	 *
	 */
	public function getClassPaths()
	{
		return $this->_classPaths;
	}
	
	/**
	 *
	 *
	 *
	 */
	public function removeClassPath($namespace)
	{
		if($namespace != __TECHNODKNAMESPACE__){
			if(isset($this->_classPaths[$namespace])){
				unset($this->_classPaths[$namespace]);
				return true;
			} else {
				return false;
			}
		} else {
			throw new Exception("Cannot remove TechnoDK namespace");
		}
	}
	
	/**
	 *
	 *
	 *
	 */
	public function removeClassPaths()
	{
		$this->_classPaths = array();
		$this->addClassPath(__TECHNODKNAMESPACE__, dirname(__TECHNODKPATH__));
	}
	
	/**
	 *
	 *
	 *
	 */
	public function loadClass($class)
	{
		if(!class_exists($class)){
			$class = str_replace('_','/',$class);
			foreach($this->_classPaths as $path){
				$path = $path . DIRECTORY_SEPARATOR . $class . '.php';
				if(file_exists($path)){
					include $path;
					return;
				}
			}
		}
	}


}



?>